using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DevHelper
{
    using SharpSoft.Data;
    using SharpSoft.Data.Expressions;
    using SharpSoft.Data.GSQL;
    using System.IO;

    /// <summary>
    /// DALGen.xaml 的交互逻辑
    /// </summary>
    public partial class DALGen : Page
    {
        public DALGen()
        {
            InitializeComponent();
            var stream = getResFile("demo.txt");
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                txt_source.AppendText(reader.ReadToEnd());
            }
        }
        protected System.IO.Stream getResFile(string name)
        {
            Type type = this.GetType();
            var _namespace = type.Namespace;
            System.Reflection.Assembly _assembly = type.Assembly;

            var resourceName = _namespace + "." + name;
            System.IO.Stream stream = _assembly.GetManifestResourceStream(resourceName);
            return stream;
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            TextRange range = new TextRange(txt_source.Document.ContentStart, txt_source.Document.ContentEnd);
            var sou = range.Text;
            GSQLAnalyzer analyzer = new GSQLAnalyzer(sou);
            try
            {
                var stas = analyzer.ReadStatements();
            }
            catch (SyntaxException ex)
            { 
                txt_source.Focus();
                var pos = ex.Token.Position;
                txt_source.Selection.Select(txt_source.Document.ContentStart.GetPositionAtOffset(pos), txt_source.Document.ContentStart.GetPositionAtOffset(pos + ex.Token.Content.Length));
                System.Windows.MessageBox.Show(ex.Message);
            }

        }
    }
}
